package com.demo.text;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.demo.common.model.Proxyip;

public class TestProxyIp {

	private List<Proxyip> proxyipList;

	public TestProxyIp(List<Proxyip> proxyipList) {
		this.proxyipList = proxyipList;
	}

	public List<Proxyip> getValidIp() throws InterruptedException {
		final List<Proxyip> validIpList = Collections.synchronizedList(new ArrayList<Proxyip>());
		ExecutorService pool;
		List<Callable<Integer>> callers = new ArrayList<Callable<Integer>>();
		if(proxyipList.size() < 50) {
			pool = Executors.newCachedThreadPool();
		} else {
			pool = Executors.newFixedThreadPool(50);
		}
		
		for (final Proxyip proxyip : proxyipList) {
			callers.add(new Callable<Integer>() {
				@Override
				public Integer call() throws Exception {
					System.setProperty("http.maxRedirects", "50");
					System.getProperties().setProperty("proxySet", "true");
					System.getProperties().setProperty("http.proxyHost", proxyip.getIpStr());
					System.getProperties().setProperty("http.proxyPort", proxyip.getPortStr());
					Document document;
					try {
						document = Jsoup.connect("http://www.ip138.com/ip2city.asp").userAgent("Mozilla")
								.cookie("auth", "token").timeout(3000).get();
						String resultStr = document.select("center").text();
						if (resultStr.substring(resultStr.indexOf("[") + 1, resultStr.indexOf("]"))
								.equals(proxyip.getIpStr())) {
							validIpList.add(proxyip);
						}
					} catch (IOException e) {
						e.printStackTrace();

					}
					return null;
				}
			});
		}
		
		pool.invokeAll(callers);
		System.out.println("ip验证完毕!");
		pool.shutdown();
		
		return validIpList;
	}

	public static void main(String[] args) throws IOException {
		/*
		 * System.setProperty("http.maxRedirects", "50");
		 * System.getProperties().setProperty("proxySet", "true"); //
		 * 如果不设置，只要代理IP和代理端口正确,此项不设置也可以 String ip = "183.139.135.172";
		 * 
		 * System.getProperties().setProperty("http.proxyHost", ip);
		 * System.getProperties().setProperty("http.proxyPort", "8080");
		 * 
		 * // 确定代理是否设置成功
		 * System.out.println(getHtml("http://www.ip138.com/ip2city.asp"));
		 */
		System.setProperty("http.maxRedirects", "50");
		System.getProperties().setProperty("proxySet", "true");
		System.getProperties().setProperty("http.proxyHost", "117.177.250.146");
		System.getProperties().setProperty("http.proxyPort", "8081");
		Document document = Jsoup.connect("http://www.ip138.com/ip2city.asp").userAgent("Mozilla")
				.cookie("auth", "token").timeout(3000).get();
		String resultStr = document.select("center").text();
		System.out.println(resultStr.substring(resultStr.indexOf("[") + 1, resultStr.indexOf("]")));

	}

	private static String getHtml(String address) {
		StringBuffer html = new StringBuffer();
		String result = null;
		try {
			URL url = new URL(address);
			URLConnection conn = url.openConnection();
			conn.setRequestProperty("User-Agent",
					"Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
			BufferedInputStream in = new BufferedInputStream(conn.getInputStream());

			try {
				String inputLine;
				byte[] buf = new byte[4096];
				int bytesRead = 0;
				while (bytesRead >= 0) {
					inputLine = new String(buf, 0, bytesRead, "ISO-8859-1");
					html.append(inputLine);
					bytesRead = in.read(buf);
					inputLine = null;
				}
				buf = null;
			} finally {
				in.close();
				conn = null;
				url = null;
			}
			result = new String(html.toString().trim().getBytes("ISO-8859-1"), "gb2312").toLowerCase();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			html = null;
		}
		return result;
	}
}
